﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReadFromFiles
{
    class Program
    {
        public static void DisplayPropertyInfo(PropertyInfo[] propInfos)
        {
            // Display information for all properties. 
            foreach (var propInfo in propInfos)
            {
                bool readable = propInfo.CanRead;


                Console.WriteLine("   Property name: {0}", propInfo.Name);


                Console.WriteLine();
            }
        }

        public static String GetVisibility(MethodInfo accessor)
        {
            if (accessor.IsPublic)
                return "Public";
            else if (accessor.IsPrivate)
                return "Private";
            else if (accessor.IsFamily)
                return "Protected";
            else if (accessor.IsAssembly)
                return "Internal/Friend";
            else
                return "Protected Internal/Friend";
        }


        private const string ControllerString = @"using Sustainalytics.ClientDW.Entities;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.ESGRatings.BusinessLogic.ESGOnlineReport;
using Sustainalytics.ESGRatings.Service.Models;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Sustainalytics.Utils;
using Sustainalytics.Utils.WebApi.ErrorCodes;
using WebApi.OutputCache.V2;

namespace Sustainalytics.ESGRatings.Service.Controllers
{{
    [AutoInvalidateCacheOutput]
    [LogControllerFilter(true, true, false)] 
    [RoutePrefix(""api/v1/company"")]
    public class {0}Controller : ApiController
    {{
        public {0}Controller(IESGRatingOnlineReportGenericManagement<{0}> service)
        {{
            _service = service;
        }}

        public {0}Controller()
        {{
            Setup();
        }}

        [Route(""{{companyId}}/{0}"")]
        public HttpResponseMessage Get(Guid companyId)
        {{
            try
            {{

                var data = _service.GetByCompanyId(companyId).Select(item => item.ToDto()).ToArray();
                if (data.IsNullOrEmpty())
                {{
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }}
                return Request.CreateResponse(HttpStatusCode.OK, new {{ data }});


            }}
            catch (Exception)
            {{
                return Request.CreateResponse(HttpStatusCode.InternalServerError,
                    new {{ ErrorStatusCode = WebAPIGeneralErrorCodes.UnhandledException }});
            }}

          
        }}

        private IESGRatingOnlineReportGenericManagement<{0}> _service;


        private void Setup()
        {{

            IRepositoryCollection<{0}> repository = new RepositoryCollection<{0}>(ClientDWConnection.GetConnectionString());

            _service = _service ?? new ESGRatingOnlineReportGenericManagement<{0}>(repository);
        }}";

        static void Main(string[] args)
        {
            const int chunkSize = 2 * 1024; // 2KB
            //var path=Console.ReadLine();
            //if (path != null)
            //{
            //string[] filePaths = Directory.GetFiles(path, "*.cs",
            //    SearchOption.AllDirectories);


            //using (var output = File.Create("output.cs"))
            //{
            //    foreach (var file in filePaths)
            //    {
            //        using (var input = File.OpenRead(file))
            //        {
            //            var buffer = new byte[chunkSize];
            //            int bytesRead;
            //            while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
            //            {
            //                output.Write(buffer, 0, bytesRead);
            //            }
            //        }
            //    }
            //}


            var result = new StringBuilder();

            // var  template= "public static {0} ToDto(this  domainModel) { return new {0}() {  }; }";
            var template = "public static {0} ToDto(this {1} domainModel) {{\r\n" +
                               " return new {0} {{\r\n" +
            "{2}\r\n" +
                                "}};\r\n" +
                               "}}\r\n";

            var templateMethod = "{0}=domainModel.{0},\r\n";

            var pathToDll = @"C:\Workspace\Client Platform\Main\Client Platform\Sustainalytics.ESGRatings.Service\bin\Debug\Sustainalytics.ESGRatings.Service.exe";
            Assembly sampleAssembly = Assembly.LoadFrom(pathToDll);

            foreach (Type tp in sampleAssembly.GetTypes())
            {
                if (tp.Name.Contains("Dto"))
                {

                    result=new StringBuilder(String.Format(ControllerString, tp.Name.Replace(@"Dto", "")));

                    using (var outfile = new StreamWriter(tp.Name.Replace(@"Dto", "")+"Controller.cs"))
                    {
                        outfile.Write(result.ToString());
                    }
                }
            }

          

        }
    }
}
